package com.etc.cure.service.Impl;

import com.etc.common.utils.Result;
import com.etc.common.utils.UUIDUtil;
import com.etc.cure.R.RExamine;
import com.etc.cure.mapper.ExamineMapper;
import com.etc.cure.service.ExamineService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.server.ExportException;
import java.util.List;

@Service
public class ExamineServiceImpl implements ExamineService {

    @Value("${file.Path}")
    private String path;

    @Resource
    private ExamineMapper examineMapper;

    @Override
    public Result queryExamine(String osId) {
        List<RExamine> list = examineMapper.queryExamine(osId);
        return Result.ok(list);
    }

    @Override
    @Transactional
    public Result uploadFile(MultipartFile file,String examineId){
        try{
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //此文件id
            String imageId = UUIDUtil.getUUID();
            String newFileName = imageId + suffixName;
            //修改数据库的图片id
            examineMapper.updateExamineImage(examineId,newFileName);
            //上传路径
            String filePath = path + newFileName;
            File dest = new File(filePath);
            file.transferTo(dest);
            return Result.ok("文件上传成功！");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("文件上传失败！");
        }
    }

    @Override
    public Result queryExamineDoc() {
        return Result.ok(examineMapper.queryExamineDoc());
    }

}
